if [ -z "$__LIB__IMPORTED__" ]; then
    export __LIB__IMPORTED__='y'
    set -e
    set -u
    export red=$(tput setaf 1)
    export green=$(tput setaf 2)
    export yellow=$(tput setaf 3)
    export blue=$(tput setaf 4)
    export purple=$(tput setaf 5)
    export cyan=$(tput setaf 6)
    export reset=$(tput sgr0)
    export bold=$(tput bold)
    export dim=$(tput dim)
    export rev=$(tput rev)
    export itm=$(tput sitm)
    export mul=$(tput smul)
    export mso=$(tput smso)
    export blink=$(tput blink)

    visit() {
        if [ -z "$1" ]; then
            exit 1
        fi
        local m=($(caller))
        m="m$(ls -i ${m[1]})"
        m=($m)
        m="${m[0]}_$1"
        local n=$1
        shift
        if declare -p $n >/dev/null 2>&1; then
            eval "echo \$$n"
            return 0
        elif declare -p $m >/dev/null 2>&1; then
            eval "echo \$$m"
            return 0
        elif declare -f $n >/dev/null 2>&1; then
            eval "$n $*"
            return 0
        elif declare -f $m >/dev/null 2>&1; then
            eval "$m $*"
            return 0
        fi
        return 1
    }

    private() {
        local m=($(caller))
        m="m$(ls -i ${m[1]})"
        m=($m)
        m="${m[0]}"
        declare -p | awk '{if($3~/^'$m'/){gsub(/'$m'_/,"local ",$3);print $3}}'
    }

    import() {
        local f=
        local env1=
        local env2=
        local met=
        local line=
        for f in "$@"; do
            if [ -f "$f" ]; then
                env1="$(declare -p | awk '{if($2=="--")print $3}' | sed -r 's/^([^=]+)=.*$/\1/g')"
                source "$f"
                env2="$(declare -p | awk '{if($2=="--")print $3}' | sed -r 's/^([^=]+)=.*$/\1/g')"
                #根据inode id生成前缀
                met=("m$(ls -i $f)")
                met=($met)
                # 重命名变量
                for line in $(echo -e "$env1\n$env2" | sort | uniq -u); do
                    # echo "unset $line;${met[0]}_$(declare -p $line | awk '{if($2=="--")print $3}')"
                    eval "unset $line;${met[0]}_$(declare -p $line | awk '{if($2=="--")print $3}')"
                done
                # 重命名函数
                for f in $(declare -F | awk -v OFS=' ' '{if($2=="-f")print $3}'); do
                    # echo "unset -f $f;"$'\n'"${met[0]}_$(declare -f $f)"
                    eval "unset -f $f;"$'\n'"${met[0]}_$(declare -f $f)"
                done
            else
                echo "Error: Cannot find library at: $f"
                exit 1
            fi
        done
    }

    trim() {
        local trimmed=$1
        trimmed=${trimmed%% }
        trimmed=${trimmed## }
        echo $trimmed
    }

    log() {
        local level=2
        local LEVEL=('?' DEBUG INFO WARNING ERROR CRITICAL)
        while getopts diwec op; do
            case $op in
            # l) level=$OPTARG ;;
            d) level=1 ;;
            i) level=2 ;;
            w) level=3 ;;
            e) level=4 ;;
            c) level=5 ;;
            esac
        done
        shift $(($OPTIND - 1))
        OPTIND=1
        local color=
        case $level in
        1) color=$yellow ;;
        2) color=$green ;;
        3) color=$rev$yellow ;;
        4) color=$red ;;
        5) color=$rev$red ;;
        esac
        if [ "${LOG_LEVEL=3}" -le "$level" ]; then
            echo "$purple $(caller) $reset[$blue $(date +"%Y-%m-%d %T") $reset] $color ${LEVEL[$level]} $reset : ${1-}"
        fi
        return 0
    }

    show() {
        echo $1 | sed -e 's/%/%-/' \
            -re 's/([0-9]+)/%4\1%0/g' \
            -re 's/([$]+)/%1\1%0/g' \
            -re 's/(["'"'"']+)/%3\1%0/g' \
            -re 's/([a-zA-Z]+)/%2\1%0/g' \
            -re 's/([*]+)/%5\1%0/g' \
            -re 's/(\|+)/%6\1%0/g' \
            -re "s/%0/$reset/g" \
            -re "s/%1/$red/g" \
            -re "s/%2/$green/g" \
            -re "s/%3/$yellow/g" \
            -re "s/%4/$blue/g" \
            -re "s/%5/$purple/g" \
            -re "s/%6/$cyan/g" \
            -e 's/%-/%/'
    }

    success() {
        echo "$(tput setaf 2)"[✔]"$(tput sgr0)"${1-}
    }

    info() {
        echo "$(tput setaf 4)"[➭]"$(tput sgr0)"${1-}
    }

    error() {
        echo "$(tput setaf 1)"[✘]"$(tput sgr0)"${1-}
        exit 1
    }

    warn() {
        echo "$(tput setaf 3)"[⚠]"$(tput sgr0)"${1-}
    }

    need() {
        if ! hash "$1" &>/dev/null; then
            error "$1 not found !"
        fi
    }

    export -f import
    export -f trim
    export -f visit
    export -f log
    export -f need
    export -f warn
    export -f error
    export -f info
    export -f success
    export -f show
    export -f log
    export -f private

    set +e
    set +u

fi